package com.tenoz.display
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import gs.TweenLite;
	import gs.easing.Expo;

	public class DeathParticle extends Sprite
	{
		public var point:Point;
		private var RADIUS:Number = 250;
		
		public function DeathParticle(_x:Number, _y:Number)
		{
			this.point = new Point(_x, _y);
			
			this.graphics.beginFill(0, 1);
			this.graphics.drawCircle(0,0,3);
			this.graphics.endFill();
			
			init();
		}
		
		public function recycle(_x:Number, _y:Number):void
		{
			this.point.x = _x;
			this.point.y = _y;
			
			init();
		}
		
		protected function init():void
		{
			this.point.x += Math.random()*2 - 1;
			this.point.y += Math.random()*2 - 1;
			
			this.alpha = 0;
			var radians:Number = Math.random()*Math.PI*2;
			this.x = Math.cos(radians)*this.RADIUS + this.point.x;
			this.y = Math.sin(radians)*this.RADIUS + this.point.y;
			
			TweenLite.to(this, 1, {x:this.point.x, y:this.point.y, alpha:1, ease:Expo.easeOut, onComplete:die});
		}
		
		protected function die():void
		{
			TweenLite.to(this, .7, {y:"150", delay:5, alpha:0, ease:Expo.easeOut, onComplete:reportDeath});
		}
		
		protected function reportDeath():void
		{
			this.dispatchEvent(new Event('dead'));
		}
	}
}